<?php

namespace addons\payment\controller;

use app\admin\model\payment\Channel;
use app\admin\model\payment\Order;
use EasyWeChat\Kernel\Contracts\Arrayable;
use think\addons\Controller;

class Payback extends Controller
{

    public function index()
    {
        $this->error("当前插件暂无前台页面");
    }

    /**
     * 异步通知
     *
     * @return void
     */
    public function notify() {
        $channel_id = $this->request->param('channel_id');
        $row = Channel::get($channel_id);
        if (!$row) {
            return $this->error('未找到渠道');
        }
        $service = $row->getService();
        $service->back($this->request->param(), $row);
    }

    /**
     * 支付回调页面
     *
     */
    public function back() {
        $channel_id = $this->request->param('channel_id');
        $row = Channel::get($channel_id);
        if (!$row) {
            return $this->error('未找到渠道');
        }
        $service = $row->getService();
        $res = $service->back($this->request->param(), $row);
        if ($res instanceof bool || $res instanceof string) {
            return $res;
        }
        if ($res instanceof Arrayable) {
            return json_encode($res, JSON_UNESCAPED_UNICODE);
        }
        return $res;
    }
}
